package com.huawei.hms.framework.network.grs.a;

import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.grs.GrsBaseInfo;
import com.huawei.hms.framework.network.grs.b.c;
import com.huawei.hms.framework.network.grs.c.e;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class a {
    private static final String a = a.class.getSimpleName();
    private static Map<String, Map<String, Map<String, String>>> b = new ConcurrentHashMap(16);
    private static Map<String, Long> c = new ConcurrentHashMap(16);

    public static String a(GrsBaseInfo grsBaseInfo, String str, String str2, b bVar) {
        Map a = a(grsBaseInfo, str, bVar);
        return a == null ? null : (String) a.get(str2);
    }

    public static Map<String, String> a(GrsBaseInfo grsBaseInfo, String str, b bVar) {
        Map map = (Map) b.get(grsBaseInfo.getGrsParasKey(false, true));
        if (map == null || map.isEmpty()) {
            return new HashMap();
        }
        a(grsBaseInfo, bVar);
        return (Map) map.get(str);
    }

    public static void a(GrsBaseInfo grsBaseInfo) {
        long j = 0;
        String grsParasKey = grsBaseInfo.getGrsParasKey(false, true);
        String a = c.a().a(grsParasKey, "");
        String a2 = c.a().a(grsParasKey + "time", "0");
        if (!TextUtils.isEmpty(a2) && a2.matches("\\d+")) {
            try {
                j = Long.parseLong(a2);
            } catch (Throwable e) {
                Logger.w(a, "convert urlParamKey from String to Long catch NumberFormatException.", e);
            }
        }
        b.put(grsParasKey, com.huawei.hms.framework.network.grs.a.b(a));
        c.put(grsParasKey, Long.valueOf(j));
        a(grsBaseInfo, grsParasKey);
    }

    private static void a(GrsBaseInfo grsBaseInfo, b bVar) {
        Long l = (Long) c.get(grsBaseInfo.getGrsParasKey(false, true));
        if (e.a(l)) {
            bVar.a(2);
            return;
        }
        if (e.a(l, 300000)) {
            com.huawei.hms.framework.network.grs.b.e.a().a(grsBaseInfo, null);
        }
        bVar.a(1);
    }

    public static void a(GrsBaseInfo grsBaseInfo, c cVar) {
        if (cVar.c() == 2) {
            Logger.w(a, "update cache from server failed");
            return;
        }
        String grsParasKey = grsBaseInfo.getGrsParasKey(false, true);
        c.a().b(grsParasKey, cVar.a());
        c.a().b(grsParasKey + "time", cVar.b());
        b.put(grsParasKey, com.huawei.hms.framework.network.grs.a.b(cVar.a()));
        c.put(grsParasKey, Long.valueOf(Long.parseLong(cVar.b())));
    }

    private static void a(GrsBaseInfo grsBaseInfo, String str) {
        Long l = (Long) c.get(str);
        Logger.v(a, "cacheTime is{%s} and its size is{%s}", c, Integer.valueOf(c.size()));
        if (e.a(l, 300000)) {
            com.huawei.hms.framework.network.grs.b.e.a().a(grsBaseInfo, null);
        }
    }

    public static void b(GrsBaseInfo grsBaseInfo) {
        String grsParasKey = grsBaseInfo.getGrsParasKey(false, true);
        c.a().b(grsParasKey + "time", "0");
        c.remove(grsParasKey + "time");
        b.remove(grsParasKey);
        com.huawei.hms.framework.network.grs.b.e.a().a(grsParasKey);
    }
}
